import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

import 'package:qaun_ni_dev/provider/gift_provider.dart';
import 'package:qaun_ni_dev/pages/gift/gift_detail.dart';

class GiftPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('礼物列表'),
      ),
      body: Container(
        child: GiftListPage()
        ),
    );
  }
}

class GiftListPage extends StatefulWidget {
  @override
  _GiftListPageState createState() => _GiftListPageState();
}

class _GiftListPageState extends State<GiftListPage> {

  GlobalKey<RefreshFooterState> _footerKey33 = new GlobalKey<RefreshFooterState>();
  GlobalKey<RefreshHeaderState> _headerKey33 = new GlobalKey<RefreshHeaderState>();


  @override
  void initState() {
    super.initState();
    _getList();

  }

  void _getList() {
    Provider.of<GiftProvider>(context,listen: false).getGiftList(context);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Consumer<GiftProvider>(
        builder: (context,data,_){
          if (data.giftListData.length > 0) {
            return Container(
              padding: EdgeInsets.only(top: 10,left: 15,right: 15),
              child: EasyRefresh(
                refreshFooter: ClassicsFooter(
                    key:_footerKey33,
                    bgColor:Colors.white,
                    textColor:Colors.pink,
                    moreInfoColor: Colors.pink,
                    showMore:true,
                    noMoreText: '没有更多了',
                    moreInfo:'加载中',
                    loadReadyText:'上拉加载'
                ),
                refreshHeader:ClassicsHeader(
                    key: _headerKey33,
                    refreshText: "下拉刷新数据",
                    refreshReadyText: "准备刷新数据",
                    refreshingText: "数据刷新中...",
                    refreshedText: "刷新完成",
                    bgColor: Colors.blue,
                    textColor: Colors.white,
                    moreInfoColor: Colors.white,
                    refreshHeight: 70.0,
                    isFloat: false,
                    showMore: false,
                    moreInfo: "Updated at %T"
                ),
                child: ListView.builder(
                    itemCount: data.giftListData.length,
                    itemBuilder: (context,index) {
                      return _itemGiftList(data.giftListData,index);
                    }
                ),
                  onRefresh: () async{
                    print('上拉刷新');
                    Provider.of<GiftProvider>(context,listen: false).getGiftList(context);
                  }


              ),
            );

          } else {
            return Center(
              child: Text('暂无数据或者数据加载中'),
            );
          }
        },
      ),
    );
  }

  Widget _itemGiftList(List newGiftList,int index) {
    return InkWell(
      child: Container(
        margin: EdgeInsets.only(bottom: 10),
        decoration: BoxDecoration(
            border: Border.all(width: 1, color: Colors.black12),
            borderRadius: BorderRadius.all(Radius.circular(6))),
        child: Row(
          children: <Widget>[
            _leftOrderNumber(newGiftList, index),
            _rightContent(newGiftList, index),
          ],
        ),
      ),
      onTap: () {
        Navigator.push(context, MaterialPageRoute(
            builder: (context) {
              return GiftDetail(newGiftList[index]);
            }
        ));
      },
    );
  }

  Widget _leftOrderNumber(newGiftList, index) {
    return Container(
      margin: EdgeInsets.only(right: 10),
      padding: EdgeInsets.only(left: 6),
      width: 50,
      child: Column(
        children: <Widget>[
          Text(
            '序号${index}',
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }

  Widget _rightContent(newGiftList, index) {
    return Container(
      padding: EdgeInsets.only(left: 8, right: 6, top: 6, bottom: 6),
      decoration: BoxDecoration(
          border: Border(
        left: BorderSide(color: Colors.grey, width: 1),
      )),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '礼物id:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${newGiftList[index].id}'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '礼物名称:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${newGiftList[index].giftName}'),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
